<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\SetRecorder;

class  SetController extends BaseController{

    public function actionGeturl(){
        $data = \Yii::$app->request->getHostInfo().\Yii::$app->request->getBaseUrl();
        $is_web = @file_get_contents("../data/install.lock");
        $is_web = intval($is_web);
        $data = $is_web?str_replace("web",'',$data):$data;
        $data = $data.'/';
        $result = ['result'=>1,'data'=>$data];
        return json_encode($result);
    }


    public function actionSave(){
        $post = \Yii::$app->request->post('form');
        if(!is_array($post)){
            $result = ['result'=>0,'data'=>'参数发生错误，请重新操作'];
            return json_encode($result);
        }
        $post['pagesize'] = intval($post['pagesize']);
        $post['pagesize'] = ($post['pagesize']<=0)?5:$post['pagesize'];
        $post['freight'] = intval($post['freight']);
        $post['freight'] = ($post['freight']<=0)?0:$post['freight'];
        $post['freight_none'] = doubleval($post['freight_none']);
        $post['freight_none'] = ($post['freight_none']<=0)?0:number_format($post['freight_none'],'2','.','');
        $post['packing'] = floatval($post['packing']);
        $post['packing'] = ($post['packing']<=0)?0:number_format($post['packing'],'2','.','');
        $post['send_time'] = floatval($post['send_time']);
        $post['send_time'] = ($post['send_time']<=0)?0.5:$post['send_time'];
        $post['send_start_time'] = intval($post['send_start_time']);
        $post['send_end_time'] = intval($post['send_end_time']);
        $post = json_encode($post);
        $model = SetRecorder::find()->where('set_type=1')->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'系统数据出现问题，请联系管理员处理'];
            return json_encode($result);
        }
        $model->updatetime = time();
        $model->content = $post;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionContent(){
        $model = SetRecorder::find()->where('set_type=1')->one();
        $data = json_decode($model->content,true);
        $result = ['result'=>1,'data'=>$data];
        return json_encode($result);
    }
}
